local skel = fk.CreateSkill {
  name = "emo__mouqi",
  tags = {Skill.Switch},
}

Fk:loadTranslationTable{
  ["emo__mouqi"] = "谋奇",
  [":emo__mouqi"] = "转换技，游戏开始时你自选阴阳状态。阳：一名角色使用【杀】时，你可以令其获得一张锦囊牌；阴：一名角色使用【闪】时，你可以令其获得一张装备牌。你对至少两名角色发动本技能的回合结束时，你可以令其中一名角色获得一个额外回合（每轮限一次）。",

  [":emo__mouqi_yang"] = "转换技，游戏开始时你自选阴阳状态。<font color=\"#E0DB2F\">阳：一名角色使用【杀】时，你可以令其获得一张锦囊牌；</font>阴：一名角色使用【闪】时，你可以令其获得一张装备牌。你对至少两名角色发动本技能的回合结束时，你可以令其中一名角色获得一个额外回合（每轮限一次）。",
  [":emo__mouqi_yin"] = "转换技，游戏开始时你自选阴阳状态。阳：一名角色使用【杀】时，你可以令其获得一张锦囊牌；<font color=\"#E0DB2F\">阴：一名角色使用【闪】时，你可以令其获得一张装备牌。</font>你对至少两名角色发动本技能的回合结束时，你可以令其中一名角色获得一个额外回合（每轮限一次）。",

  ["emo__mouqi_yang"] = "谋奇:阳",
  ["emo__mouqi_yin"] = "谋奇:阴",
  ["#emo__mouqi-start"] = "你可以选择“谋奇”的阴阳状态",
  ["#emo__mouqi-give"] = "谋奇：你可以令 %dest 获得一张 %arg",
  ["#emo__mouqi-extra"] = "谋奇：你可以令一名角色获得一个额外回合",
}

--- 设置阴阳状态并切换皮肤
---@param player ServerPlayer
local function SetSwitchSkillState(player, state)
  state = state or player:getSwitchSkillState(skel.name)
  if player:getSwitchSkillState(skel.name, true) == state then
    player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. skel.name, state)
    player:setSkillUseHistory(skel.name, player:usedSkillTimes(skel.name, Player.HistoryGame), Player.HistoryGame)
  end
  -- 切换皮肤
  local generals = {"moemou__emoprincess", "moemou2__emoprincess"}
  local general = state == fk.SwitchYang and generals[1] or generals[2]
  if Fk.generals[player.general].trueName == "emoprincess" then
    player.room:setPlayerProperty(player, "general", general)
  end
  if Fk.generals[player.deputyGeneral] and Fk.generals[player.deputyGeneral].trueName == "emoprincess" then
    player.room:setPlayerProperty(player, "general", general)
  end
end

skel:addEffect(fk.CardUsing, {
  anim_type = "switch",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and not target.dead then
      if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
        return data.card.trueName == "slash"
      else
        return data.card.trueName == "jink"
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local t = (data.card.trueName == "slash" and "trick" or "equip")
    if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__mouqi-give::" .. target.id .. ":" .. t }) then
      event:setCostData(self, { tos = {target}, type = t })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    SetSwitchSkillState(player)
    room:addTableMarkIfNeed(player, "emo__mouqi-turn", target.id)
    local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..event:getCostData(self).type)
    if #ids > 0 then
      room:obtainCard(target, ids, false, fk.ReasonJustMove, target, skel.name)
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player:getTableMark("emo__mouqi-turn") > 1
    and player:usedEffectTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, pid in ipairs(player:getTableMark("emo__mouqi-turn")) do
      local p = room:getPlayerById(pid)
      if p:isAlive() then
        table.insert(targets, p)
      end
    end
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      skill_name = skel.name,
      targets = targets,
      min_num = 1,
      max_num = 1,
      prompt = "#emo__mouqi-extra",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, MarkEnum.SwithSkillPreName .. skel.name, player:getSwitchSkillState(skel.name, true))
    player:addSkillUseHistory(skel.name, -1)
    local to = event:getCostData(self).tos[1] ---@type ServerPlayer
    to:gainAnExtraTurn(true, skel.name)
  end,
})

--- 开局自选阴阳状态
skel:addEffect(fk.GameStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askToChoice(player, {
      choices = { "emo__mouqi_yang", "emo__mouqi_yin" },
      skill_name = skel.name,
      prompt = "#tymou_switch-choice:::xianmou",
    })
    local state = choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin
    SetSwitchSkillState(player, state)
  end,
})

return skel
